package cn.darkhorse.ticket.store.service.verification.ICalculationService;

import cn.darkhorse.ticket.store.dto.TicketProductDetailsDTO;
import cn.darkhorse.ticket.store.pojo.TicketInventorySurplus;
import cn.darkhorse.ticket.store.service.TicketInventorySurplusService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @description:
 * @author: mr.wang
 * @time: 2024/1/18 11:14
 */
public class MaterialUsage{
	
	
	@Resource
	public TicketInventorySurplusService surplusService;
	

	
	public ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
	
	
	
	/**
	 * 使用记录最新一条
	 * @return
	 */
	public Map<String, TicketInventorySurplus> getSurplusMap() {
		rwl.readLock().lock();
		String inSql = String.format("SELECT MAX(tis.id) as id FROM ticket_inventory_surplus tis  " +
				"GROUP BY tis.product_id,tis.warehouse_id DESC");
		QueryWrapper<TicketInventorySurplus> queryWrapper = new QueryWrapper<>();
		queryWrapper.inSql("id", inSql);
		List<TicketInventorySurplus> surplusList = surplusService.list(queryWrapper);
		rwl.readLock().unlock();
		Map<String, TicketInventorySurplus> surplusMap = new HashMap<>(surplusList.size());
		surplusList.forEach(item -> {
			surplusMap.put(item.getWarehouseId()+"-"+item.getProductId(), item);
		});
		return surplusMap;
	}
	
	
	
	/**
	 * 拼接数量校验对象
	 * @param details
	 * @return
	 */
	public Map<String,TicketProductDetailsDTO> surplusCheckDTOMap(List<TicketProductDetailsDTO> details){
		Map<String,TicketProductDetailsDTO> surplusMap = new HashMap<>();
		details.forEach(item->{
			surplusMap.put(item.getWarehouseInfoId()+"-"+item.getProductId(),item);
		});
		return surplusMap;
	}
	
	
	
	
	
	
}
